package cn.itcast.zt.controller;

import cn.itcast.zt.domain.User;
import cn.itcast.zt.repository.UserRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * create by zhangtian on 2018/3/5
 */
@RestController
@RequestMapping
public class UserController {
    // 采用构造器的注入方式，这样写好处：1、不能修改；2、可以尽快初始化
    private final UserRepository userRepository ;

    @Autowired
    public UserController(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    @PostMapping("persons/save")
    public User save(@RequestParam String name) {
        User user = new User() ;
        user.setName(name);

        if(userRepository.save(user)){
            System.out.printf("用户对象：%s 保存成功！\n", user);
        }

        return user;
    }
}
